//
//  PassageVC.m
//  Zhiche-Carriage
//
//  Created by LeeBruce on 16/12/6.
//  Copyright © 2016年 LeeBruce. All rights reserved.
//
#define cellH 50 * kHeight

#import "PassageVC.h"
#import "TopBackView.h"
#import "PassageCell.h"
#import "Common.h"
#import "RootViewController.h"
#import "NullView.h"


@interface PassageVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) NullView *nullView;


@end

@implementation PassageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    TopBackView *topBackView= [[TopBackView alloc]initViewWithFrame:CGRectMake(0, 0, screenWidth, 64) andTitle:@"在途信息"];
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    
    
    topBackView.rightButton.hidden = YES;
    [topBackView.leftButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:topBackView];
    
    self.view.backgroundColor = WhiteColor;
    
    self.dataArray = [NSMutableArray array];
    
    self.nullView = [[NullView  alloc]initWithFrame:CGRectMake(0, 64, screenWidth, screenHeight  - 64) andTitle:@"暂无信息" andImage:[UIImage imageNamed:@"no_order"]];
    self.nullView.backgroundColor = GrayColor;

    
    [self initDataSource];
    
    [self initTableView];
}

-(void)initDataSource
{
    NSString *string = [NSString stringWithFormat:@"%@?id=%@",sub_logistics,self.orderId];
   
    [Common requestWithUrlString:string contentType:application_json finished:^(id responseObj) {
        
        if ([responseObj[@"data"] isEqual:[NSNull null]]) {
            
            [self.view addSubview:self.nullView];

        } else {
        
        if ([responseObj[@"data"] count] > 0 ) {
            self.dataArray = responseObj[@"data"];
            
            [self.nullView removeFromSuperview];
            
            [self.tableView reloadData];
        }
        else {
            
            [self.view addSubview:self.nullView];
            
        }
            
        }
        
        
    } failed:^(NSString *errorMsg) {
        
        NSLog(@"%@",errorMsg);
    }];

    
}


-(void)initTableView
{
    self.dataArray = [NSMutableArray array];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, screenWidth, screenHeight - 64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc]init];
    [self.view addSubview:self.tableView];
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 30, 0, 0);
    
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *string = [NSString stringWithFormat:@"Cell%ld%ld", [indexPath section], [indexPath row]];//以indexPath来唯一确定cell
    PassageCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (!cell) {
        cell = [[PassageCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:string];
    }
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (indexPath.row == 0) {
        cell.topLabel.hidden = YES;
    }
    
    if (indexPath.row == self.dataArray.count - 1) {
        cell.bottomLabel.hidden = YES;
        
    }
    
    if (self.dataArray.count > 0) {
        
        if (indexPath.row == 0) {
            
            cell.middleImg.image = [UIImage imageNamed:@"circle"];
            cell.titleL.textColor = YellowColor;
            cell.timeLabel.textColor = YellowColor;
            
        }
        
        cell.titleL.text = [NSString stringWithFormat:@"%@",self.dataArray[indexPath.row][@"location"]];
        
        cell.timeLabel.text = [NSString stringWithFormat:@"%@",self.dataArray[indexPath.row][@"createTime"]];

    }
    
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return cellH;
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = GrayColor;
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(18, 0, 300, cellHeight)];
    label.text = [NSString stringWithFormat:@"订单号 %@",self.orderNumber];
    label.textColor = littleBlackColor;
    label.font = Font(14);
    [view addSubview:label];
    
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return cellHeight;
}


-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)viewWillAppear:(BOOL)animated{
    
    
    RootViewController *rootVC = [RootViewController defaultsTabBar];
    [rootVC setTabBarHidden:YES];
    
  }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
